<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class BaseService
{
    //基础服务

    protected $db; //表对象

    protected $dbUser = 'user_wx'; //用户
    protected $dbUlevel = 'user_level'; //用户等级
    protected $dbUtrace = 'user_trace'; //浏览历史
    protected $dbUaddress = 'user_address'; //收货地址
    protected $dbUcoupon = 'coupon_uid'; //优惠劵
    protected $dbPay = 'pay'; //支付
    //商城
    protected $dbScCate = 'goods_cate'; //商城分类
    protected $dbScGoods = 'goods'; //商城商品
    protected $dbScGsku = 'goods_sku'; //商城商品SKU
    protected $dbScOrder = 'order'; //商城订单
    protected $dbScOsku = 'order_sku'; //商城订单SKU
    //渠道
    protected $dbQdCate = 'distributor_goods_cate'; //渠道分类
    protected $dbQdGoods = 'distributor_goods'; //渠道商品
    protected $dbQdGsku = 'distributor_goods_sku'; //渠道商品SKU
    protected $dbQdOrder = 'distributor_order'; //渠道订单
    protected $dbQdOsku = 'distributor_order_sku'; //渠道订单SKU
    protected $dbQdAddress = 'distributor_address'; //收货地址

    protected $imgDomain = 'https://tiepi.qzwlyx.com';

    //订单状态
    protected $orderStatus1s = [
        1 => '未付款', '未发货', '未收货', '已完成',
    ];
    //订单状态
    protected $orderStatus2s = [
        1 => '未付款', '门店自提', '自提成功',
    ];
    //快递选项
    protected $orderDeliverys = [
        1 => '第三方快递', '到店取货',
    ];
    //是否取消
    protected $orderCancels = [
        1 => '正常', '审核中', '售后',
    ];
    //渠道订单状态
    protected $qdOrderStatuss = [
        1 => '未付款', '未发货', '未收货', '已完成',
    ];

    //返回参数
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }

    /**
     * 连接数据库
     * @param string $db 表名
     * @param array $ws 查询条件【key=>val】
     */
    public function dbConn($db, $ws=[])
    {
        $db = DB::connection('mysql')->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }

    /**
     * 连接二期数据库，外包使用
     */
    public function dbwbConn($db, $ws=[])
    {
        $db = DB::connection('mysql2')->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }
}